﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using System.Text.RegularExpressions;

namespace Koolwired.Imap.ForDummies
{
    /// <summary>
    /// A simple class for managing the mailboxmessage attachment.
    /// Keeps a reference to the asociated MailboxMessage and can therefor manage it's own operations in a
    /// true OO manner.
    /// </summary>
    public class MailboxMessageAttachment
    {
        internal MailboxMessage mailboxMessage;
        internal ImapMessageBodyPart imapMessageBodyPart;

        /// <summary>
        /// Instantiates the MailboxMessageAttachment class and sets internal references to the associated message and bodypart
        /// </summary>
        /// <param name="mailboxMessage">The MailboxMessage associated with the attachment</param>
        /// <param name="imapMessageBodyPart">The ImapMessageBodyPart associated with the attachment</param>
        internal MailboxMessageAttachment(MailboxMessage mailboxMessage, ImapMessageBodyPart imapMessageBodyPart)
        {
            this.mailboxMessage = mailboxMessage;
            this.imapMessageBodyPart = imapMessageBodyPart;
        }

        /// <summary>
        /// By default the string representation of the attachment will be its filename
        /// </summary>
        /// <returns>The filename of the attachment</returns>
        public override string ToString()
        {
            if (imapMessageBodyPart != null)
            {
                return imapMessageBodyPart.FileName;
            }
            return base.ToString();
        }

        /// <summary>
        /// Saves the attachment to the path passed as 'pathPrefix' -parameter 
        /// </summary>
        /// <param name="pathPrefix">Specifies the filelocation where the attachment will be saved, and provides a prefix to the filename. 
        /// For example: "C:\Attachments\message123_"</param>
        /// <returns>Result if the operation was succesfull</returns>
        public bool Save(string pathPrefix)
        {
            mailboxMessage.mailbox.imapCommand.FetchBodyPart(mailboxMessage.imapMailboxMessage, imapMessageBodyPart);
            try
            {
                using (FileStream fs = new FileStream(pathPrefix + imapMessageBodyPart.FileName, FileMode.Create))
                {
                    if (imapMessageBodyPart.ContentEncoding == BodyPartEncoding.BASE64)
                    {
                        byte[] data = imapMessageBodyPart.DataBinary;
                        fs.Write(data, 0, data.Length);
                        fs.Flush();
                        fs.Close();
                    }
                    else 
                    {
                        string decoded = mailboxMessage.DecodeMimeString(imapMessageBodyPart.Data);
                        StreamWriter sw = new StreamWriter(fs);
                        sw.Write(decoded);
                        sw.Close();
                    }
                    return true;
                }
            }
            catch
            {
                return false;
            }
        }

        /// <summary>
        /// Gets or sets the file name of an attachment.
        /// </summary>
        public string FileName
        {
            set { imapMessageBodyPart.FileName = value; }
            get { return imapMessageBodyPart.FileName; }
        }
        /// <summary>
        /// Gets or sets the content type of an body type.
        /// </summary>
        public System.Net.Mime.ContentType ContentType
        {
            set { imapMessageBodyPart.ContentType = value; }
            get { return imapMessageBodyPart.ContentType; }
        }
        /// <summary>
        /// Gets or sets the size of body part.
        /// </summary>
        public long Size
        {
            set { imapMessageBodyPart.Size = value; }
            get { return imapMessageBodyPart.Size; }
        }
    
    }
}
